package com.qxsx.midi.activity.exam;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.qxsx.midi.R;
import com.qxsx.midi.activity.base.BaseActivity;
import com.qxsx.midi.adapter.TimeChooseWheelAdapter;
import com.qxsx.midi.config.AppContext;
import com.qxsx.midi.config.AppManager;
import com.qxsx.midi.utils.CustomProgress;
import com.qxsx.midi.utils.MyNet;
import com.qxsx.midi.utils.SPUtils;
import com.qxsx.midi.utils.Tools;
import com.qxsx.midi.utils.Utils;
import com.qxsx.midi.utils.callBackJsonArray;
import com.qxsx.midi.wheel.OnWheelChangedListener;
import com.qxsx.midi.wheel.WheelView;
import com.qxsx.midi.wheel.WheelViewg;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * Created by Joney on 2015/9/10.
 * 报名
 */
public class ApplyActivity extends BaseActivity implements OnClickListener {
    private CustomProgress customProgress;

    private RelativeLayout rl_1, rl_2, rl_3;
    private Button btn_choose_time;
    private TextView tv_grade, tv_subject;

    private TextView tv_address;//地址

    String[] grade = {"等级1", "等级2", "等级3", "等级4", "等级5", "等级6", "等级7", "等级8", "等级9"};
    String[] musicgrade = {"初级", "中级", "高级"};

    ScrollView scrollView;
    WheelView passw_1, passw_2, passw_3, passw_4;
    String[] months_big = {"1", "3", "5", "7", "8", "10", "12"};
    String[] months_little = {"2", "4", "6", "9", "11"};
    final List<String> list_big = Arrays.asList(months_big);
    final List<String> list_little = Arrays.asList(months_little);
    String date;//当前日期


    String[] month = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"};

    String[] data1 = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30"
    };

    String[] data2 = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"
    };

    //非闰年
    String[] data3 = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28"
    };
    //闰年
    String[] data4 = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29"
    };


    String[] house = {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16",
            "17", "18", "19", "20", "21", "22", "23", "00"
    };

    String[] minute = {"20", "40", "00"
    };

    private List<String> data_list;
    private ArrayAdapter<String> arr_adapter;
    private Spinner sp_year;
    private String YEAR = "2015";//设置默认的初始年份

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        setTitle("报名");
        setNavBtn(R.mipmap.back, 0);


        SPUtils.put(AppContext.getContext(), "grade_value", "等级1");
        findById();
        inItData();

    }

    private void inItData() {
        //数据
        data_list = new ArrayList<String>();
        data_list.add("2016");
        data_list.add("2017");
        data_list.add("2018");

        //适配器
        arr_adapter = new ArrayAdapter<String>(this, R.layout.item_timer_spinner, data_list);
        //设置样式
        arr_adapter.setDropDownViewResource(R.layout.item_timer_spinner);
        //加载适配器
        sp_year.setAdapter(arr_adapter);
        sp_year.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                YEAR = data_list.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        passw_1.setAdapter(new TimeChooseWheelAdapter(month));
        passw_1.setCurrentItem(0);

        passw_1.setCyclic(true);
        passw_1.setVisibleItems(3);
        passw_1.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    scrollView.requestDisallowInterceptTouchEvent(false);
                } else {
                    scrollView.requestDisallowInterceptTouchEvent(true);

                }
                return false;
            }
        });

        passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
        passw_2.setCurrentItem(0);
        /**
         * 月份
         */
        passw_1.addChangingListener(new OnWheelChangedListener() {
            @Override
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                if (Tools.isLeapYear(Integer.valueOf(YEAR.trim()))) {

                    Log.e("湿润年", YEAR);
                    switch (wheel.getCurrentItemValue()) {
                        case "01":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "02":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data4));
                            passw_2.setCurrentItem(0);
                            break;
                        case "03":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "04":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data1));
                            passw_2.setCurrentItem(0);
                            break;
                        case "05":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "06":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data1));
                            passw_2.setCurrentItem(0);
                            break;
                        case "07":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "08":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "09":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data1));
                            passw_2.setCurrentItem(0);
                            break;
                        case "10":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "11":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data1));
                            passw_2.setCurrentItem(0);
                            break;
                        case "12":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                    }
                } else {
                    Log.e("非闰年", YEAR);
                    switch (wheel.getCurrentItemValue()) {
                        case "01":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "02":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data3));
                            passw_2.setCurrentItem(0);
                            break;
                        case "03":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "04":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data1));
                            passw_2.setCurrentItem(0);
                            break;
                        case "05":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "06":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data1));
                            passw_2.setCurrentItem(0);
                            break;
                        case "07":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "08":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "09":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data1));
                            passw_2.setCurrentItem(0);
                            break;
                        case "10":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                        case "11":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data1));
                            passw_2.setCurrentItem(0);
                            break;
                        case "12":
                            passw_2.setAdapter(new TimeChooseWheelAdapter(data2));
                            passw_2.setCurrentItem(0);
                            break;
                    }
                }
            }

            @Override
            public void onChanged(WheelViewg wheel, int oldValue, int newValue) {

            }
        });


        passw_2.setCyclic(true);
        passw_2.setVisibleItems(3);
        passw_2.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    scrollView.requestDisallowInterceptTouchEvent(false);
                } else {
                    scrollView.requestDisallowInterceptTouchEvent(true);
                }
                return false;
            }
        });


//        String grade_value = (String) SPUtils.get(ApplyActivity.this, "grade_value", "等级1");
//        tv_grade.setText(grade_value);


    }

    @Override
    protected void onResume() {
        super.onResume();
        applytime();
        String grade_subject = (String) SPUtils.get(ApplyActivity.this, "subject_name", "电吉他");
        String choos_address = (String) SPUtils.get(ApplyActivity.this, "choos_address", "");

        tv_subject.setText(grade_subject);
        tv_address.setText(choos_address);
        if (tv_subject.getText().toString().equals("音乐理论")){
            SPUtils.put(AppContext.getContext(), "grade_value", "初级");
            SPUtils.put(ApplyActivity.this, "grade_newvalue", 0);
            SPUtils.put(AppContext.getContext(), "apply_grade_value", "1");
            tv_grade.setText("初级");

        }else{
            SPUtils.put(AppContext.getContext(), "grade_value", "等级1");
            SPUtils.put(ApplyActivity.this, "grade_newvalue", 0);
            SPUtils.put(AppContext.getContext(), "apply_grade_value", "1");
            tv_grade.setText("等级1");
        }
    }




    private void applytime() {
        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        try {
            object.put("param", "no");
            array.put(object);

        } catch (JSONException e) {
            e.printStackTrace();
        }
        MyNet.doPostJsonArray("502", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                try {
                    JSONObject object1 = array.getJSONObject(0);
                    date = object1.getString("date");
                    Log.w("date", date);

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

    }


    private void findById() {
        rl_1 = (RelativeLayout) findViewById(R.id.rl_1);
        rl_1.setOnClickListener(this);
        rl_2 = (RelativeLayout) findViewById(R.id.rl_2);
        rl_2.setOnClickListener(this);
        rl_3 = (RelativeLayout) findViewById(R.id.rl_3);
        rl_3.setOnClickListener(this);
        tv_grade = (TextView) findViewById(R.id.tv_grade);
        tv_subject = (TextView) findViewById(R.id.tv_subject);

        tv_address = (TextView) findViewById(R.id.tv_address);
        btn_choose_time = (Button) findViewById(R.id.btn_choose_time);
        btn_choose_time.setOnClickListener(this);

        scrollView = (ScrollView) findViewById(R.id.scrollView);
        passw_1 = (WheelView) findViewById(R.id.passw_1);
        passw_2 = (WheelView) findViewById(R.id.passw_2);

        sp_year = (Spinner) findViewById(R.id.sp_year);

    }

    @Override
    protected void HandleTitleBarEvent(TitleBar component, View v) {

        switch (component) {
            case LEFT:
                finish();
                break;

        }
    }


    private void apply() {
        customProgress = CustomProgress.show(ApplyActivity.this, "请稍后....", true, null);

        JSONArray array = new JSONArray();
        JSONObject object = new JSONObject();
        String apply_major_id = (String) SPUtils.get(ApplyActivity.this, "apply_major_id", "2172405d-17bb-5f40-da92-cf9cc285a980");
        String user_id = (String) SPUtils.get(AppContext.getContext(), "user_id", "");
        String apply_grade_value = (String) SPUtils.get(ApplyActivity.this, "apply_grade_value", "1");
        try {
            object.put("user_id", user_id);
            object.put("grade_num", apply_grade_value);
            object.put("major_id", apply_major_id);
            array.put(object);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        MyNet.doPostJsonArray("505", array, new callBackJsonArray() {
            @Override
            public void run(Object code, Object message, JSONArray array) {
                if (code.toString().equals("200")) {
                    SPUtils.put(AppContext.getContext(), "exam_time", YEAR + "-" + passw_1.getCurrentItemValue() + "-" + passw_2.getCurrentItemValue());
                    SPUtils.put(AppContext.getContext(), "exam_year", YEAR);
                    SPUtils.put(AppContext.getContext(), "exam_month", passw_1.getCurrentItemValue());
                    SPUtils.put(AppContext.getContext(), "exam_day", passw_2.getCurrentItemValue());
                    AppManager.getAppManager().addActivity(ApplyActivity.this);
                    startActivity(new Intent(AppContext.getContext(), ExamChooseTimeActivity.class));


                } else if (code.toString().equals("422")) {
                    ShowToast(message.toString());
                } else if (code.toString().equals("430")) {
                    ShowToast(message.toString());
                } else if (code.toString().equals("421")) {
                    ShowToast(message.toString());
                }
                customProgress.dismiss();
            }
        });


    }


    //选择后三天
    private boolean ischeck() {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //得到指定模范的时间
        try {
            Date d1 = sdf.parse(YEAR + "-" + passw_1.getCurrentItemValue() + "-" + passw_2.getCurrentItemValue());
            Date d2 = sdf.parse(Tools.getDateStr(date, 3));
            if (d1.getTime() > d2.getTime()) {
                return true;
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }


        return false;
    }


    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.rl_1:
                startActivity(new Intent(ApplyActivity.this, ChooseAddressActivity.class));
                break;

            case R.id.rl_2:
                startActivity(new Intent(ApplyActivity.this, ExamSubjectActivity.class));
                break;
            case R.id.rl_3:
                LayoutInflater layout = LayoutInflater.from(AppContext.getContext());
                final View view = layout.inflate(R.layout.dialog_exam_subject, null);
                final Dialog dialog = new Dialog(ApplyActivity.this, R.style.dialog);
                WheelViewg whl_subject = (WheelViewg) view.findViewById(R.id.whl_subject);
                if (tv_subject.getText().toString().equals("音乐理论")) {
                    whl_subject.setAdapter(new TimeChooseWheelAdapter(musicgrade));

                } else {
                    whl_subject.setAdapter(new TimeChooseWheelAdapter(grade));
                }
                Button btn_sure = (Button) view.findViewById(R.id.btn_sure);

                int gv = (int) SPUtils.get(ApplyActivity.this, "grade_newvalue", 0);
                whl_subject.setCurrentItem(gv);

                whl_subject.setCyclic(true);
                whl_subject.setVisibleItems(3);
                whl_subject.addChangingListener(new OnWheelChangedListener() {

                    @Override
                    public void onChanged(WheelView wheel, int oldValue, int newValue) {

                    }

                    @Override
                    public void onChanged(WheelViewg wheel, int oldValue, int newValue) {
                        tv_grade.setText(wheel.getCurrentItemValue());
                        if (tv_subject.getText().toString().equals("音乐理论")) {
                            SPUtils.put(AppContext.getContext(), "grade_value", wheel.getCurrentItemValue());
                            if (wheel.getCurrentItemValue().equals("初级")){
                                SPUtils.put(ApplyActivity.this, "grade_newvalue", 0);
                                SPUtils.put(AppContext.getContext(), "apply_grade_value", "1");
                            }else if (wheel.getCurrentItemValue().equals("中级")){
                                SPUtils.put(ApplyActivity.this, "grade_newvalue", 1);
                                SPUtils.put(AppContext.getContext(), "apply_grade_value", "2");
                            }else if (wheel.getCurrentItemValue().equals("高级")){
                                SPUtils.put(ApplyActivity.this, "grade_newvalue", 2);
                                SPUtils.put(AppContext.getContext(), "apply_grade_value", "3");
                            }
                        } else {
                            SPUtils.put(AppContext.getContext(), "grade_value", wheel.getCurrentItemValue());
                            Log.w("xxxx", wheel.getCurrentItemValue() + "");
                            SPUtils.put(ApplyActivity.this, "grade_newvalue", newValue);
                            Log.w("xxxx", newValue + "");


                            String s = new String(wheel.getCurrentItemValue());
                            String a[] = s.split("级");
                            SPUtils.put(AppContext.getContext(), "apply_grade_value", a[1]);
                            Log.w("xxxx", a[1] + "");
                        }
                    }
                });

                DisplayMetrics dm = new DisplayMetrics();
                getWindowManager().getDefaultDisplay().getMetrics(dm);
                int x = dm.widthPixels;// 屏幕的宽
                int y = dm.heightPixels;// 屏幕的宽

                dialog.getWindow().setLayout((int) (x / 2.2), (int) (y / 1.8));

                dialog.show();
                dialog.setContentView(view);
                //dialog.setCancelable(false);
                btn_sure.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                    }
                });
                break;

            case R.id.btn_choose_time:
                if (tv_address.getText().toString().equals("")) {
                    ShowToast("请选择考试地点");

                } else if (ischeck()) {
                    apply();
                } else {
                    ShowToast("请选择" + Tools.getDateStr(date, 3) + "之后的时间报名");
                }


                break;


            default:
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SPUtils.put(ApplyActivity.this, "grade_newvalue", 0);
    }
}
